package com.hwg.shiliupro.service.Impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hwg.shiliupro.dao.NoteMapper;
import com.hwg.shiliupro.po.Note;
import com.hwg.shiliupro.service.NoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class NoteServiceImpl implements NoteService {

    @Autowired
    private NoteMapper noteMapper;

    /**
     * 查找笔记集合通过用户id  查自己
     * @param uId 你id
     * @return {@link List}<{@link Note}>
     */
    @Override
    public List<Note> selectByUserId(Integer uId) {
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_id",uId);
        List<Note> notes = noteMapper.selectList(queryWrapper);
        log.info("查询到的笔记集合是：{}",notes);
        return notes;
    }

    /**
     * 查找笔记集合通过用户id  查别人
     * @param uId 你id
     * @return {@link List}<{@link Note}>
     */
    @Override
    public List<Note> selectByUserIdAndVisible(Integer uId) {
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("u_id",uId);
        queryWrapper.eq("visible",1);
        List<Note> notes = noteMapper.selectList(queryWrapper);
        log.info("查询到的笔记集合是：{}",notes);
        return notes;
    }


    /**
     * 通过笔记id查询笔记内容 查自己
     *
     * @param id id
     * @return {@link Note}
     */
    @Override
    public Note selectByNoteId(Integer id) {
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        Note note = noteMapper.selectOne(queryWrapper);
        return note;
    }

    /**
     * 通过笔记id查询笔记内容 查别人 + visible == 1
     *
     * @param id id
     * @return {@link Note}
     */
    @Override
    public Note selectByNoteIdAndVisible(Integer id) {
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.eq("visible",1);   //1代表笔记开放
        Note note = noteMapper.selectOne(queryWrapper);
        return note;
    }

    /**
     * 删除笔记通过id
     *
     * @param id id
     * @return {@link Boolean}
     */
    @Override
    public Boolean deleteById(Integer id) {
        int count = noteMapper.deleteById(id);
        return count > 0;
    }

    /**
     * 新增笔记
     *
     * @param note 请注意
     * @return {@link Note}
     */
    @Override
    public int insert(Note note) {
        log.info("新增笔记的内容为：{}",note);
        int count = noteMapper.insert(note);
        //System.out.println(note.getId()); 能够主键返回
        return note.getId();
    }

    /**
     * 修改笔记
     *
     * @param note 请注意
     * @return {@link Boolean}
     */
    @Override
    public Boolean updateNote(Note note) {
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",note.getId());
        int count = noteMapper.update(note, queryWrapper);
        return count > 0;
    }


}
